//
//  ASHomeHeadMenuView.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/27.
//

#import "ASHomeHeadMenuView.h"
#import "ASDoutuController.h"
#import "ASMakeAnimeMainVC.h"
#import "ASAvatarClassController.h"
#import "ASTextMainVC.h"

static NSString *ASHomeHeadMenuSubCellStr = @"ASHomeHeadMenuSubCell";

@interface ASHomeHeadMenuModel : NSObject
@property (strong, nonatomic) NSString *titleStr;
@property (strong, nonatomic) NSString *imageName;
@end
@implementation ASHomeHeadMenuModel
@end

@interface ASHomeHeadMenuSubCell : UICollectionViewCell
@property (strong, nonatomic) UIImageView *iconImage;
@property (strong, nonatomic) UILabel *titleLab;
@end

@implementation ASHomeHeadMenuSubCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.iconImage];
        [self.contentView addSubview:self.titleLab];
        [self setAllViewLayout];
    }
    return self;
}

- (void)setAllViewLayout{
    @weakify(self);
    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.mas_equalTo(16);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(46, 46));
    }];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.mas_equalTo(self.iconImage.mas_bottom).mas_offset(12);
        make.centerX.mas_equalTo(self.mas_centerX);
    }];
}

- (UIImageView *)iconImage{
    if (!_iconImage) {
        _iconImage = [UIImageView new];
        _iconImage.contentMode = UIViewContentModeScaleAspectFill;
        _iconImage.clipsToBounds = YES;
    }
    return _iconImage;
}

- (UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.font = [UIFont systemFontOfSize:13];
        _titleLab.textColor = [UIColor colorWithHexString:@"#4D4D4D"];
    }
    return _titleLab;
}
@end

@implementation ASHomeHeadMenuView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createData];
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
        [self.collectionView reloadData];
    }
    return self;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *fl = [UICollectionViewFlowLayout new];
        fl.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        fl.minimumLineSpacing = 0;
        fl.minimumInteritemSpacing = 0;
        fl.itemSize = CGSizeMake((SCREEN_WIDTH - 20) / self.dataArr.count, 102);
        fl.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:fl];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = self.backgroundColor;
        [_collectionView registerClass:[ASHomeHeadMenuSubCell class] forCellWithReuseIdentifier:ASHomeHeadMenuSubCellStr];
    }
    return _collectionView;
}

- (void)createData{
    self.dataArr = [NSMutableArray new];
    NSArray *imageArr = @[@"home_flei",@"home_txiang",@"home_wan",@"home_dtu"];
    NSArray *titleArr = @[@"头像分类",@"漫画头像",@"个性文案",@"斗图表情"];
    for (int x = 0; x < titleArr.count; x++) {
        ASHomeHeadMenuModel *model = [ASHomeHeadMenuModel new];
        model.titleStr = titleArr[x];
        model.imageName = imageArr[x];
        [self.dataArr addObject:model];
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ASHomeHeadMenuSubCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ASHomeHeadMenuSubCellStr forIndexPath:indexPath];
    if (self.dataArr.count > indexPath.row) {
        ASHomeHeadMenuModel *model = self.dataArr[indexPath.row];
        cell.titleLab.text = model.titleStr;
        [cell.iconImage setImage:[UIImage imageNamed:model.imageName]];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            [self pushAvatarClassVC];
            break;
        case 1:
            [self pushMakeAnimVC];
            break;
        case 2:
            [self pushTextVC];
            break;
        case 3:
            [self pushDoutuVC];
            break;
        case 4:
//            [self pushImageAiVC:IGImageAIType_Clear];
            break;
        default:
            break;
    }
}

///头像分类页面
- (void)pushAvatarClassVC{
    ASAvatarClassController *avatarVC  = [ASAvatarClassController new];
    avatarVC.hidesBottomBarWhenPushed = YES;
    [currViewController().navigationController pushViewController:avatarVC animated:YES];
}

///跳转个性文字页面
- (void)pushTextVC{
    ASTextMainVC *textVC = [ASTextMainVC new];
    textVC.hidesBottomBarWhenPushed = YES;
    [currViewController().navigationController pushViewController:textVC animated:YES];
}

///跳转动漫表情制作页面
- (void)pushMakeAnimVC{
    [ASTool addUMTongJiKey:@"home" subKey:@"tool" titleStr:@"漫画头像"];
    ASMakeAnimeMainVC *makeVC = [ASMakeAnimeMainVC new];
    makeVC.hidesBottomBarWhenPushed = YES;
    [currViewController().navigationController pushViewController:makeVC animated:YES];
}

///跳转斗图表情页面
- (void)pushDoutuVC{
    ASDoutuController *doutuVC = [ASDoutuController new];
    doutuVC.hidesBottomBarWhenPushed = YES;
    [currViewController().navigationController pushViewController:doutuVC animated:YES];
}

@end
